<?php

class Core_Models_DbTable_Games extends Zend_Db_Table_Abstract {

    protected $_name = "games";
    protected $_rowClass = "Core_Models_Game";
    static private $_instance;

    public static function getInstance() {
        if (!(self::$_instance instanceof Core_Models_DbTable_Games)) {
            self::$_instance = new Core_Models_DbTable_Games();
        }
        return self::$_instance;
    }

    public function getGames($params) {
        $select = $this->select()
                ->setIntegrityCheck(FALSE)
                ->where('id_tour = ?', $params['id_tour']);
        $games = $this->fetchAll($select);
        return $games;
    }
    
    public function getSelectGames($params) {
        $select = $this->select()
                ->setIntegrityCheck(FALSE)
                ->from(array('games'))
                ->where('id_tour = ?', $params['id_tour']);
        if (!empty($params['season'])) {
            $select->where('season = ?', mysql_escape_string($params['season']));
        }
        if (!empty($params['round'])) {
            $select->where('round = ?', $params['round']);
        }
        if (!empty($params['id_club'])) {
            $id_club = $params['id_club'];
            $select->where("id_club1 = $id_club OR id_club2 = $id_club");
        }
        return $select;
    }
    
    public function getSeasons($params){
        $select = $this->select()
                ->setIntegrityCheck(FALSE)
                ->from(array('games'),array('season'))
                ->where('id_tour = ?', $params['id_tour'])
                ->where('season IS NOT NULL')
                ->group('season');
        $games = $this->fetchAll($select);
        
        return $games;
    }
    
    public function getRounds($params){
        $select = $this->select()
                ->setIntegrityCheck(FALSE)
                ->from(array('games'),array('round'))
                ->where('id_tour = ?', $params['id_tour'])
                ->where('round IS NOT NULL')
                ->group('round')
                ->order('round ASC');
        $games = $this->fetchAll($select);
        
        return $games;
    }
    
    public function getTournaments() {
        $select = $this->select()
                ->setIntegrityCheck(FALSE)
                ->from(array('games'),array('id_tour'))
                ->joinLeft('tournaments', 'tournaments.id = games.id_tour', array('id','name'))
                ->group('id_tour');
        $games = $this->fetchAll($select);
        
        return $games;
    }
}